#include <board.h>
#include <rtthread.h>
#include "drv_spi.h"

void spi_slave_thread(void *parameter)
{
    struct rt_spi_device *spidev;

    rt_hw_spi_device_attach(BUS_IF_SPI_NAME, BUS_IF_NAME, GPIOF, GPIO_PIN_6);
    spidev = (struct rt_spi_device *)rt_device_find(BUS_IF_NAME);

    struct rt_spi_configuration cfg;
    cfg.data_width = 8;
    cfg.mode = RT_SPI_SLAVE | RT_SPI_MODE_1 | RT_SPI_MSB | RT_SPI_NO_CS; /* SPI Compatible Modes 0 */
    cfg.max_hz = 2 * 1000 * 1000; /* SPI Interface with Clock Speeds Up to 20 MHz */
    rt_spi_configure(spidev, &cfg);
    rt_spi_take_bus(spidev);

    recvbuf = rt_malloc(128);

    while(1) {
        rt_memset(recvbuf, 0, recv_sz);
        recv_sz = rt_spi_recv(spidev, recvbuf, 1024);
        rt_thread_mdelay(5);
    }
}
